package cn.tedu._05mvcboot02.controller;

import cn.tedu._05mvcboot02.common.response.JsonResult;
import cn.tedu._05mvcboot02.common.response.StatusCode;
import cn.tedu._05mvcboot02.mapper.UserMapper;
import cn.tedu._05mvcboot02.pojo.dto.AddUserDTO;
import cn.tedu._05mvcboot02.pojo.entity.User;
import cn.tedu._05mvcboot02.pojo.vo.UserListVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * RestController注解:
 *   1.组合注解:相当于 Controller + ResponseBody;
 *   2.在控制器类上添加此注解后,所有的控制器方法上都无需再添加 @ResponseBody 注解了.
 */

//@Controller
@Slf4j
@RestController
@RequestMapping(value = "/v1/users/")
@Api("用户模块")
public class UserController {
    @Autowired
    private UserMapper userMapper;
    /*1.添加用户功能*/
    @PostMapping(value = "insert")
    @ApiOperation("添加用户")
    public JsonResult advdUser(AddUserDTO addUserDTO){
        log.debug("addUserDTO = " + addUserDTO);
        User user = new User();
        BeanUtils.copyProperties(addUserDTO,user);
        user.setCreated(new Date());
        userMapper.insertUser(user);
        return JsonResult.ok();
    }
    /*查找一条数据*/
    @GetMapping(value = "user")
    @ApiOperation("查找一条数据")
    @ApiImplicitParam(name = "id",value = "用户id",required = true,dataType = "int")
    public JsonResult selectUser(int id){
//        System.out.println("id = " + id);
        log.debug("id = " + id);
        User user = userMapper.selectUserById(id);
        return JsonResult.ok(user);
    }
    /*查找所有的数据*/
    @GetMapping(value = "userList")
    @ApiOperation("查找所有的数据")
    public JsonResult userList(){
        //梳理业务逻辑
        List<UserListVO> userListVOS = userMapper.selectUserList();
        return JsonResult.ok(userListVOS);
    }
    /*删除一条数据*/
    @DeleteMapping(value = "delete")
    @ApiOperation("删除一条数据")
    @ApiImplicitParam(name = "id",value = "用户ID",required = true,dataType = "int")
    public  JsonResult deleteUser(int id){
//        System.out.println("id = " + id);
        if (id<0){
            throw new IllegalArgumentException("id不能小于0");
        }
        log.debug("id = " + id);
        int d = userMapper.deleteUserByID(id);
        if (d>0){
            return JsonResult.ok();
        }else {
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
    }
    /*更新一条数据*/
    @PutMapping(value = "update")
    @ApiOperation("更新一条数据")
    public JsonResult updateUser(User user){
//        System.out.println("user = " + user);
        log.debug("user = " + user);
        userMapper.updateUser(user);
        return JsonResult.ok();
    }
    /*
        根据用户ID查询指定用户信息
     */
    @GetMapping(value = "{id}")
    @ApiOperation("查询指定用户信息")
    @ApiImplicitParam(name = "id",value = "用户ID",required = true,dataType = "int")
    public JsonResult getUserByID(@PathVariable Integer id){
        User user = userMapper.selectUserById(id);
        return JsonResult.ok(user);
    }
}
